﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using ImageDemo.CommData;

namespace ImageDemo.PictureBoxDemo
{
    public partial class MergePicture : Form
    {
        public MergePicture()
        {
            InitializeComponent();
        }

        private void btnUp_Click(object sender, EventArgs e)
        {
            if (pbRight.Location.Y > 5)
            {
                pbRight.Location = new Point(pbRight.Location.X, pbRight.Location.Y - 5);
                pbRight.Refresh();
            }

        }

        private void btnLeft_Click(object sender, EventArgs e)
        {
            if (pbRight.Location.X > 5)
            {
                pbRight.Location = new Point(pbRight.Location.X - 5, pbRight.Location.Y);
                pbRight.Refresh();
            }
        }

        private void btnDown_Click(object sender, EventArgs e)
        {
            if (pbRight.Location.Y + pbRight.Height + 5 < plMergeArea.Height)
            {
                pbRight.Location = new Point(pbRight.Location.X, pbRight.Location.Y + 5);
                pbRight.Refresh();
            }
        }

        private void btnRight_Click(object sender, EventArgs e)
        {
            if (pbRight.Location.X + pbRight.Width + 5 < plMergeArea.Width)
            {
                pbRight.Location = new Point(pbRight.Location.X + 5, pbRight.Location.Y);
                pbRight.Refresh();
            }
        }

        private void btnLeftUp_Click(object sender, EventArgs e)
        {
            if (pbLeft.Location.Y > 5)
            {
                pbLeft.Location = new Point(pbLeft.Location.X, pbLeft.Location.Y - 5);
                pbLeft.Refresh();
            }
        }

        private void btnLeftleft_Click(object sender, EventArgs e)
        {
            if (pbLeft.Location.X > 5)
            {
                pbLeft.Location = new Point(pbLeft.Location.X - 5, pbLeft.Location.Y);
                pbLeft.Refresh();
            }
        }

        private void btnLeftDown_Click(object sender, EventArgs e)
        {
            if (pbLeft.Location.Y + pbLeft.Height + 5 < plMergeArea.Height)
            {
                pbLeft.Location = new Point(pbLeft.Location.X, pbLeft.Location.Y + 5);
                pbLeft.Refresh();
            }
        }

        private void btnLeftRight_Click(object sender, EventArgs e)
        {
            if (pbLeft.Location.X + pbLeft.Width + 5 < plMergeArea.Width)
            {
                pbLeft.Location = new Point(pbLeft.Location.X + 5, pbLeft.Location.Y);
                pbLeft.Refresh();
            }
        }

        private void btnMerge_Click(object sender, EventArgs e)
        {
            Bitmap bmp = new Bitmap(pbResult.Width, pbResult.Height);

            Graphics g = Graphics.FromImage(bmp);
            //g.DrawImage(pbLeft.Image, new Point(0, 0));
            //g.CopyFromScreen(this.Location.X + plMergeArea.Location.X, this.Location.Y + plMergeArea.Location.Y,
            //    this.Location.X + plMergeArea.Location.X + plMergeArea.Width, this.Location.Y + plMergeArea.Location.Y + plMergeArea.Height,
            //    new Size(plMergeArea.Width, plMergeArea.Height), CopyPixelOperation.SourcePaint);

            //bmp.LockBits(new Rectangle(0,0,bmp.Width,bmp.Height), System.Drawing.Imaging.ImageLockMode.ReadWrite, System.Drawing.Imaging.PixelFormat.Max, null);

            //bmp.Save(@"d:\result.bmp", System.Drawing.Imaging.ImageFormat.Tiff);

            //string strBasePath = AppDomain.CurrentDomain.BaseDirectory;
            //string strTemp = strBasePath.Substring(0, strBasePath.IndexOf(@"demo\"));

            //pbLeft.SizeMode = PictureBoxSizeMode.AutoSize;
            //pbResult.Image = Image.FromHbitmap(bmp.GetHbitmap());
            //pbResult.Invalidate();

            Dictionary<DrawOrder, ImageInfo> dicImageInfo = ConstructImageInfo();
            if (dicImageInfo == null || dicImageInfo.Count != 2)
            {
                return;
            }
            ImageInfo imgFirst = dicImageInfo[DrawOrder.First];
            //g.DrawImage(pbResult.Image, new Rectangle(imgFirst.InitPosition, new Size(imgFirst.Width, imgFirst.Height)),
            g.DrawImage(pbLeft.Image, new Rectangle(imgFirst.Location, new Size(imgFirst.Width, imgFirst.Height)),
                new Rectangle(imgFirst.InitPosition, new Size(imgFirst.Width, imgFirst.Height)), GraphicsUnit.Pixel);
            ImageInfo imgSecond = dicImageInfo[DrawOrder.Second];
            g.DrawImage(pbRight.Image, new Rectangle(imgSecond.Location, new Size(imgSecond.Width, imgSecond.Height)),
                new Rectangle(imgSecond.InitPosition, new Size(imgSecond.Width, imgSecond.Height)), GraphicsUnit.Pixel);
            //g.DrawImage(pbLeft.Image, new Point(0, 0));
            //g.DrawImage(pbRight.Image, new Point(pbLeft.Image.Width, 0));

            bmp.Save(@"d:\temp\result.bmp");

            pbResult.Image = bmp;
            pbResult.Invalidate();
        }

        private ImageInfo ConstructImageInfo(PictureBox pbTemp, Panel plTemp, DrawOrder order)
        {
            ImageInfo imgInfo = new ImageInfo();

            int xPos = 0;
            int yPos = 0;
            int xLoc = pbTemp.Location.X;
            int yLoc = pbTemp.Location.Y;
            int width = pbTemp.Width;
            int height = pbTemp.Height;

            if (xLoc < 0)
            {
                xPos = Math.Abs(xLoc);
                width = width - xPos;
                xLoc = 0;
            }
            if (yLoc < 0)
            {
                yPos = Math.Abs(yLoc);
                height = height - yPos;
                yLoc = 0;
            }
            if (pbTemp.Location.X + pbTemp.Width > plTemp.Width)
            {
                width = width - (pbTemp.Location.X + pbTemp.Width - plTemp.Width);
            }
            if (pbTemp.Location.Y + pbTemp.Height > plTemp.Height)
            {
                height = height - (pbTemp.Location.Y + pbTemp.Height - plTemp.Height);
            }

            imgInfo.InitPosition = new Point(xPos, yPos);
            imgInfo.Location = new Point(xLoc, yLoc);
            imgInfo.Width = width;
            imgInfo.Height = height;
            imgInfo.DrawOrder = order;

            return imgInfo;
        }

        private Dictionary<DrawOrder, ImageInfo> ConstructImageInfo()
        {
            Dictionary<DrawOrder, ImageInfo> dicImageInfo = new Dictionary<DrawOrder, ImageInfo>();
            dicImageInfo.Add(DrawOrder.First, ConstructImageInfo(pbLeft, plMergeArea, DrawOrder.First));
            dicImageInfo.Add(DrawOrder.Second, ConstructImageInfo(pbRight, plMergeArea, DrawOrder.Second));

            return dicImageInfo;
        }

        private void MergePicture_Load(object sender, EventArgs e)
        {
            string strBasePath = AppDomain.CurrentDomain.BaseDirectory;
            string strTemp = strBasePath.Substring(0, strBasePath.IndexOf(@"demo\"));

            pbLeft.SizeMode = PictureBoxSizeMode.AutoSize;            
            string strLeftImgPath = strTemp + @"demo\data\head.jpg";
            pbLeft.ImageLocation = strLeftImgPath;
            pbLeft.Load();

            pbRight.SizeMode = PictureBoxSizeMode.AutoSize;
            string strRightImgPath = strTemp + @"demo\data\cover.jpg";
            pbRight.ImageLocation = strRightImgPath;
            pbRight.Load();

            pbResult.SizeMode = PictureBoxSizeMode.AutoSize;
            //pbResult.Location = plResult.Location;
            //pbResult.Width = plResult.Width;
            //pbResult.Height = plResult.Height;
        }
    }
}
